Previous Book Contents Book Index Next

Inside Macintosh: QuickDraw GX Objects /
Chapter 1 - Introduction to QuickDraw GX / Objects and Memory


Sharing and Multiple Object References

Object-based systems can use large amounts of memory, especially when an application needs to create and use many objects. To minimize redundancy and excess memory use, QuickDraw GX supports the sharing of objects.

For example, you may want to create a set of shape objects that are of different sizes and geometries, but that all have the same color and are drawn with the same transfer mode. You can create a single ink object with the desired color and transfer mode that all the shapes can reference, without having to create a separate ink object for each shape. In that situation there is one reference to the ink object for each shape that uses it.

Alternatively, your application can create data structures that contain object references, and two or more structures can contain references to the same object. For example, different palette structures can contain references to the same color set object that defines the palette colors. In that situation there is one reference to the color set object for each palette that uses it.

Sharing is not the same as making a copy. No matter how many references there are to an object, it is still only a single object. When you change any aspect of a shared object, those changes are reflected in every other object or data structure that references that object.

Object sharing provides at least three advantages:

Owner Count

The current number of references to an object is called its owner count. QuickDraw GX tracks and manages owner counts for you, so in most cases you needn't worry about how many references there are to an object and whether or not to delete it from memory when you no longer need it in a given context.

When you first create an object (with a call such as GXNewStyle), QuickDraw GX gives it an initial owner count of 1. Whenever you attach that object to another object (with
a call such as GXSetShapeStyle), QuickDraw GX does not duplicate it; instead, it increases the object's owner count by 1. Whenever you delete that object (with a call
such as GXDisposeStyle) or any object that references it (with a call such as GXDisposeShape), QuickDraw GX decreases its owner count by 1.

QuickDraw GX uses the owner count to determine when an object is no longer
needed and can be deleted. If at any time the object's owner count decreases to zero, QuickDraw GX deletes it from QuickDraw GX memory. As far as your application is concerned, you create and dispose of objects as you wish, and let QuickDraw GX decide when to actually remove them from memory.

There can be cases, however, in which the owner count would normally become 0 but you do not want the object to be deleted. In those cases, you can increase owner count with the cloning capability of QuickDraw GX, described next.

Cloning

Although QuickDraw GX can correctly track owner counts as objects are created, disposed of, and referenced from other objects, it cannot know how many references to a given object exist in variables and data structures that you have created. In these situations, it is up to you to manage the owner counts of the objects that you use. Also, you may want to preserve a reference to an object that QuickDraw GX disposes of when it disposes of or modifies another object. In such a case, you can make sure the owner count of an object correctly reflects the number of references to it by cloning the object, which means increasing its owner count.

For example, if you create a color set object, it has an owner count of 1. If you dispose of that color set, its owner count becomes zero and it is deleted by QuickDraw GX, as it should be. On the other hand, if you assign a new ink object to a shape, that shape's original ink object is disposed of and the owner count of the new ink object is increased by 1. If you had wanted to maintain a reference to the shape's original ink object, you could have cloned that ink before assigning the new ink to the shape. The original ink's owner count would remain above zero, and it would therefore not be deleted.

As another example, you may temporarily change the style object assigned to a shape, intending to restore that style to the shape eventually. When you assign the new style object, QuickDraw GX decrements the original style object's owner count because it is no longer used by the shape. If the original style is not used by another object, its owner count would become 0 and QuickDraw GX would delete it. To prevent that from occurring, you can clone the original style object before assigning the new one.

QuickDraw GX cannot determine when you are finished with an object once it is cloned. If you clone an object, you are responsible for disposing of it when it is no longer needed.

Some Objects Cannot Be Cloned
Some objects have no owner count because they need to be able to be deleted even when valid references to them remain. View-related objects (view ports, view devices, and view groups) and fonts are examples of such shared objects that cannot be cloned. For example, suppose a transform object references a particular view port object associated with a window. When the application closes the window, it disposes of the view port. The view port object is deleted, even though a valid reference to it still remains in the transform object. (Subsequent drawing to that view port reference has no effect; QuickDraw GX ignores references to a view-related object that does not exist.)

Previous Book Contents Book Index Next

© Apple Computer, Inc.
7 JUL 1996